import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import Cookie from '@/plugins/cookie';

const routes: Array<RouteRecordRaw> = [
  {
    /* 404页面 */
    name: '404',
    path: '/:routePath(.+)+',
    component: () => import('@/views/404.vue'),
    meta: {
      title: '404',
    },
  },
  {
    /* 登录页面 */
    name: 'login',
    path: '/login',
    component: () => import('@/views/login.vue'),
    meta: {
      title: '登录',
    },
  },
  {
    /* 注册页面 */
    name: 'register',
    path: '/register',
    component: () => import('@/views/register/index.vue'),
    meta: {
      title: '注册',
    },
  },
  {
    /* 设置用户信息页面 */
    name: 'setUserInfo',
    path: '/setUserInfo',
    component: () => import('@/views/setUserInfo.vue'),
    meta: {
      title: '设置信息',
    },
    beforeEnter() {
      const token: string = Cookie.getCookie('token');
      if (!token) return { name: 'login', replace: true };
    },
  },
  {
    /* 首页 */
    name: 'index',
    path: '/',
    component: () => import('@/views/index.vue'),
    meta: {
      title: 'TYChat',
    },
    children: [
      {
        /** 消息页面 */
        name: 'messages',
        path: '',
        component: () => import('@/components/messages/index.vue'),
        children: [
          {
            /** 具体的聊天记录 */
            name: 'chatRecord',
            path: 'chatRecord',
            component: () => import('@/components/messages/colRight/index.vue'),
            props: (route) => ({ userId: route.query.userId }),
            beforeEnter(to) {
              if (!to.query?.userId) return { name: 'messages' };
            },
          },
        ],
      },
      {
        /** 通讯录页面 */
        name: 'contact',
        path: 'contact',
        component: () => import('@/components/contact/index.vue'),
        children: [
          {
            /** 陌生朋友申请加好友列表组件 */
            name: 'applyAddFriList',
            path: 'applyAddFriList',
            component: () => import('@/components/contact/colRight/applyFriends/index.vue'),
          },
          {
            /** 通讯管理 */
            name: 'commManage',
            path: 'commManage',
            component: () => import('@/components/contact/colRight/commManage/index.vue'),
            children: [
              {
                name: 'commManageFriends',
                path: 'commManageFriends/:title',
                props: (route) => ({
                  type: route.query.type,
                  title: route.params.title,
                }),
                component: () => import('@/components/contact/colRight/commManage/friendsList.vue'),
                beforeEnter(to) {
                  switch (to.params.title) {
                    case 'all': case 'disabledOtherLook':
                    case 'disabledSelfLook': break;
                    case 'groups': {
                      if (!to.query?.type) return { name: 'commManage' };
                      else break;
                    }
                    default: return { path: '/404' };
                  }
                },
              },
            ],
          },
          {
            /** 单个好友信息页面 */
            name: 'friendInfo',
            path: 'friendInfo/:initials([A-Z~]{1})',
            component: () => import('@/components/contact/colMiddle/friends/friend.vue'),
            props: (route) => ({
              otherId: route.query.userId,
              initials: route.params.initials,
            }),
            beforeEnter(to) {
              if (!to.query?.userId) return { name: 'contact' };
            },
          },
        ],
      },
    ],
    beforeEnter() {
      const token: string = Cookie.getCookie('token');
      if (!token) return { name: 'login', replace: true };
    },
  },
]

const router = createRouter({
  // linkExactActiveClass: 'active',
  history: createWebHistory(),
  routes,
  /* scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }, */
})

router.beforeEach((to): void => {
  // 进入该路由前改变文档标题
  document.title = to.meta.title;
})

export default router